package com.warmlight.voicepacket.ui.communitylm.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.http.HttpUtils;
import com.warmlight.voicepacket.http.ResponseData;
import com.warmlight.voicepacket.ui.communitylm.adapter.ReportRecyclerAdapter;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 举报评论或者帖子
 * new reportdialog 之后调用setid 设置type 和 id   不然举报信息缺失，报错
 */
public class ReportDialog extends AppCompatDialogFragment implements View.OnClickListener {

    private ReportRecyclerAdapter adapter;
    private String type ;
    private String id;

    public void setID(String type,String id){
        this.type = type;
        this.id = id;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getContext(), R.style.share_dialog);
        dialog.setContentView(R.layout.dialog_report);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        setCancelable(true);


        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.dialogAnim3);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(attributes);


        initView(dialog);
        initData();

        return dialog;

    }

    private void initView(Dialog dialog) {
        ImageView iv_cancel = dialog.findViewById(R.id.iv_cancel);
        TextView tv_report = dialog.findViewById(R.id.tv_report);
        RecyclerView rc_report = dialog.findViewById(R.id.rc_report);

        iv_cancel.setOnClickListener(this);
        tv_report.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rc_report.setLayoutManager(linearLayoutManager);
        adapter = new ReportRecyclerAdapter(getContext());
        rc_report.setAdapter(adapter);

//        0=>"低俗色情", 1 => "政治敏感", 2 => "违法犯罪", 3 => "广告营销", 4 => "侮辱谩骂", 5 => "引起不适", 5 => "其它不当内容"
//        String[] reportmsg = new String[]{ "低俗色情",  "政治敏感",  "违法犯罪", "广告营销","侮辱谩骂", "引起不适", "其它不当内容"};
//        List<Map<String,String>> data = new ArrayList<>();
//        for (int i=0;i<=5;i++){
//            Map<String,String> map = new HashMap<>();
//            map.put("id",i+"");
//            map.put("msg",reportmsg[i]);
//            map.put("select","0");
//            data.add(map);
//        }
//        adapter.setItems(data);
    }

    /**
     * {"code":200,"time":1536201229,"msg":"",
     * "data":[{"id":0,"reason_desc":"低俗色情"},{"id":1,"reason_desc":"政治敏感"},
     * {"id":2,"reason_desc":"违法犯罪"},{"id":3,"reason_desc":"广告营销"},
     * {"id":4,"reason_desc":"侮辱谩骂"},{"id":5,"reason_desc":"引起不适"},
     * {"id":6,"reason_desc":"其它不当内容"}]}
     */
    private void initData(){
        String url = HttpConfig.GET_REPORT;
        TreeMap<String,String> treeMap = new TreeMap<>();
        HttpClient.getInstance().postRxAndroidAES(url,treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (HttpUtils.isCodeSuccess(responseData.getCode())){
                            String data = responseData.getData();
                            ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(data);
                            for (int i=0;i<listMapByJson.size();i++){
                                listMapByJson.get(i).put("select","0");
                            }
                            adapter.setItems(listMapByJson);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void report(){
        if (adapter != null){
            List<Map<String, String>> items = adapter.getItems();
            StringBuffer stringBuffer = new StringBuffer();
            if (items != null && items.size()>0){
                for (int i=0;i<items.size();i++){
                    Map<String, String> map = items.get(i);
                    String select = map.get("select");
                    String id = map.get("id");
                    if (TextUtils.equals("1",select)){
                        stringBuffer.append(id);
                        stringBuffer.append(",");
                    }
                }
            }else{
                ToastUtil.showShort(getContext(),"获取举报信息失败");
                return;
            }

            if (stringBuffer.length()>0){
                String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
                report(substring);
            }else{
                ToastUtil.showShort(getContext(),"请选择举报信息");
            }

        }
    }

    private void report(String msg){
        String url = HttpConfig.REPORT;
        TreeMap<String,String> treeMap = new TreeMap<>();
        treeMap.put("type",type);//举报类型1帖子2评论0用户
        treeMap.put("be_id",id);//对应的帖子评论用户id
        treeMap.put("tag",msg);
        HttpClient.getInstance().postRxAndroidAES(url,treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (HttpUtils.isCodeSuccess(responseData.getCode())){
                            ToastUtil.showShort(getContext(),"举报成功");
                            dismiss();
                        }else{
                            ToastUtil.showShort(getContext(),responseData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(getContext(),"网络异常，请稍后重试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_cancel:
                dismiss();
                break;
            case R.id.tv_report:
                report();
                break;


        }
    }
}
